
/*
 *
 * Copyright (c) 2006 P.J.Leonard
 * 
 * http://www.frinika.com
 * 
 * This file is part of Frinika.
 * 
 * Frinika is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * Frinika is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with Frinika; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package uk.ac.bath.domains.sequence;

import uk.ac.bath.base.Value;
import uk.ac.bath.util.MyRandom;

/**
 *
 * @author pjl
 */
public class SequenceGenerator {

    int cnt = 0;
    int len;
    int wid;
    Value[][] data;

    public SequenceGenerator(int len, int wid) {
        this.wid = wid;
        this.len = len;
        data = new Value[len][];
        for (int i = 0; i < len; i++) {
            data[i] = new Value[wid];
            for (int j = 0; j < wid; j++) {
                data[i][j] = new Value(2 * MyRandom.nextInt(2) - 1);
            }
        }
        if (isEasy()) {
            reset();
        }
    }

    public Value[] getNext() {
        if (cnt >= len) {
            cnt = 0;
        }
        return data[cnt++];
    }
    ;

    void reset() {
        cnt = 0;

        do {
            for (int i = 0; i < len; i++) {
                for (int j = 0; j < wid; j++) {
                    data[i][j].val = 2 * MyRandom.nextInt(2) - 1;
                }
            }
        } while (isEasy());
    }

    boolean isEasy() {

        if (len == 1) return false; // avoid getting stuck

        for (int i = 1; i < len; i++) {
            for (int j = 0; j < wid; j++) {
                if (data[0][j].val != data[i][j].val) {
                    return false;
                }
            }
        }

        return true;

    }

    public static void main(String args[]) {
        int wid = 1;
        SequenceGenerator g = new SequenceGenerator(3, wid);

        for (int i = 0; i < 8; i++) {
            Value[] v = g.getNext();

            for (int j = 0; j < wid; j++) {
                System.out.print(v[j].val + " ");
            }
            System.out.println();
        }

    }
}
